package com.sylar.happygps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.sylar.happygps.adapter.FaviNavAdapter;

public class FaviNavActivity extends Activity implements OnClickListener, OnItemClickListener {
	private FaviNavAdapter faviNavAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.favinavlist);

		Button btn_title_left = (Button) findViewById(R.id.btn_title_left);
		btn_title_left.setText(R.string.app_name);
		btn_title_left.setVisibility(View.VISIBLE);
		btn_title_left.setOnClickListener(this);

		TextView tv_title_name = (TextView) findViewById(R.id.tv_title_name);
		tv_title_name.setText("导航收藏");

		Button btn_edit = (Button) findViewById(R.id.btn_edit);
		btn_edit.setOnClickListener(this);
		btn_edit.setVisibility(View.VISIBLE);

		ListView list_favinav = (ListView) findViewById(R.id.list_favinav);
		list_favinav.setOnItemClickListener(this);
		faviNavAdapter = new FaviNavAdapter(this);
		list_favinav.setAdapter(faviNavAdapter);

		// 搜索
		EditText ed_search = (EditText) findViewById(R.id.ed_search);
		ed_search.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH) {
					Intent intent = new Intent(getApplicationContext(), SearchResultActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);
				}
				return false;
			}
		});
		
		View btn_search = findViewById(R.id.btn_search);
		btn_search.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(),SearchResultActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);				
			}
		});

		ed_search.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				Toast.makeText(getApplicationContext(), arg0, 0).show();
			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_title_left:
			finish();
			break;
		case R.id.btn_edit:
			if (faviNavAdapter != null) {
				if (faviNavAdapter.getDel()) {
					faviNavAdapter.setDel(false);
					faviNavAdapter.notifyDataSetChanged();
				} else {
					faviNavAdapter.setDel(true);
					faviNavAdapter.notifyDataSetChanged();
				}
			}

			break;
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		toMap();
	}

	// 地图
	private void toMap() {
		Intent intent = new Intent(this, MapActivity.class);
		startActivity(intent);
	}
}
